package com.kingmed.kmss.admin.modules.ums.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kingmed.kmss.admin.modules.ums.model.UmsAdmin;
import com.kingmed.kmss.admin.modules.ums.model.UmsAdminOrganizationRelation;
import com.kingmed.kmss.admin.modules.ums.model.UmsOrganization;
import com.kingmed.kmss.admin.qc.AdminQc;
import com.kingmed.kmss.common.dto.admin.ums.OrganizationDTO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 后台组织机构表 Mapper 接口
 * </p>
 */
public interface UmsOrganizationMapper extends BaseMapper<UmsOrganization> {

    List<UmsAdmin> adminsInOrg(Long orgId);

    Page<UmsAdmin> adminsInOrgAndChild(Page<UmsAdmin> page, List<Long> orgIds, AdminQc adminQc);

    Page<UmsAdmin> adminsInOrgAndChildNew(Page<UmsAdmin> page, List<Long> orgIds, AdminQc adminQc);

    List<UmsAdmin> adminsInOrgAndChild(List<Long> orgIds, AdminQc adminQc);

    /**
     * 用户=子公司上级 向下找子公司
     *
     * @param orgId   用户orgId
     * @param orgType * 1	集团
     *                * 2	大区
     *                * 3	子公司
     *                * 4	部门
     *                * 5	区域
     *                * 6	岗位
     *                * 7	人员
     */
    List<UmsOrganization> getDownSubsidiary(Long orgId, Integer orgType);

    /**
     * 物理删除
     */
    @Delete("delete from ums_admin_organization_relation WHERE admin_id = #{adminId}")
    int deleteIgnoreLogic(UmsAdminOrganizationRelation relation);

    @Select(" <script> " +
            " select org_id as id,org_name,kmcs_code,kmcs_name,org_level as level,org_short_name as orgShortName " +
            " from ums_kmcs_org" +
            " where 1=1" +
            " <if test=\"orgLevel != null \"> " +
            "   and org_level = #{orgLevel} " +
            " </if>" +
            " order by sort" +
            " </script> ")
    List<OrganizationDTO> groupRegionBranch(Integer orgLevel);

    List<UmsOrganization> getRegionsBySql();
}
